package com.chenshang.service.intl;

import java.util.Map;

import com.chenshang.util.JaxbUtil;
import com.travelzen.flight.base.TzRequest;
import com.travelzen.flight.base.TzResponse;
import com.travelzen.flight.international.FlightSearchRequest;
import com.travelzen.flight.international.FlightSearchResponse;
import com.travelzen.flight.international.FlightSegmentResult;
import com.travelzen.flight.international.FsFlightSegment;
import com.travelzen.flight.international.PassengerTypeEnum;
import com.travelzen.flight.international.PriceTaxVerificationRequest;
import com.travelzen.flight.international.PtvFlightScheduled;

public class CreatPriceCheckRequest implements Runnable {
	String request, response;
	Map<String, String> result;

	public String getRequest() {
		return request;
	}

	public Map<String, String> getResult() {
		return result;
	}

	public void setResult(Map<String, String> result) {
		this.result = result;
	}

	public void setRequest(String request) {
		this.request = request;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	@Override
	public void run() {

		TzRequest tzRequest = JaxbUtil.convertToTzRequestByXml(request);
		FlightSearchRequest flightSearchRequest = (FlightSearchRequest) tzRequest.getRequestEntity();
		TzResponse tzResponse = JaxbUtil.convertToTzResponseByXml(response);
		FlightSearchResponse flightSearchResponse = (FlightSearchResponse) tzResponse.getResponseEntity();
		StringBuffer priceTaxVerificationReq = new StringBuffer();
		for (FlightSegmentResult flightSegmentResult : flightSearchResponse.getFlightSegmentResult()) {

			PriceTaxVerificationRequest priceTaxVerificationRequest = new PriceTaxVerificationRequest();

			priceTaxVerificationRequest.setAirlineCompany(flightSegmentResult.getAirlineCompany());
			priceTaxVerificationRequest.setCabinRank(flightSegmentResult.getSegmentList().get(0).getCabinRank());
			priceTaxVerificationRequest.setFlightRangeType(flightSearchRequest.getFlightRangeType());
			priceTaxVerificationRequest.setFreightLimitQueryID(flightSegmentResult.getFreightRuleQueryID());
			priceTaxVerificationRequest.setPassengerNature(flightSearchRequest.getPassengerNature());

			if (flightSegmentResult.getPolicyReturnPoint().get(0).getPassengerType().equals(PassengerTypeEnum.ADU)) {
				priceTaxVerificationRequest.setPolicyID(flightSegmentResult.getPolicyReturnPoint().get(0).getPolicyId());
			} else {
				priceTaxVerificationRequest.setChdPolicyID(flightSegmentResult.getPolicyReturnPoint().get(0).getPolicyId());
			}

			for (FsFlightSegment fsFlightSegmen : flightSegmentResult.getSegmentList()) {
				for (com.travelzen.flight.international.FsFlightScheduled fsFlightScheduled : fsFlightSegmen.getFlightScheduled()) {

					PtvFlightScheduled flightScheduled = new PtvFlightScheduled();

					flightScheduled.setArriveTime(fsFlightScheduled.getToDate());
					flightScheduled.setCabin(fsFlightSegmen.getCabinCode());
					flightScheduled.setDepartTime(fsFlightScheduled.getFromDate());
					flightScheduled.setFlightNo(fsFlightScheduled.getFlightNo());
					flightScheduled.setFromAirport(fsFlightScheduled.getFromAirport());
					flightScheduled.setToAirport(fsFlightScheduled.getToAirport());

					priceTaxVerificationRequest.getFlightSchedule().add(flightScheduled);
				}
			}

			tzRequest.setRequestEntity(priceTaxVerificationRequest);

			priceTaxVerificationReq.append(JaxbUtil.convertToXml(tzRequest)).append("\n");

		}
		result.put("priceCheckRequest", priceTaxVerificationReq.toString());
	}
}
